package com.zenithmind.gateway.config;

import com.zenithmind.gateway.security.SecurityProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;

/**
 * 网关安全配置类
 * 专门负责Spring Security的配置
 * 遵循单一职责原则：只负责Spring Security框架的配置
 */
@Slf4j
@Configuration
@EnableWebFluxSecurity
@RequiredArgsConstructor
@EnableConfigurationProperties(SecurityProperties.class)
public class SecurityConfig {

    /**
     * 配置Spring Security过滤器链
     * 专门负责Spring Security框架的配置
     */

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        log.info("配置Spring Security过滤器链");

        http
            .csrf(ServerHttpSecurity.CsrfSpec::disable)
            .httpBasic(ServerHttpSecurity.HttpBasicSpec::disable)
            .formLogin(ServerHttpSecurity.FormLoginSpec::disable)
            .logout(ServerHttpSecurity.LogoutSpec::disable);

        // 允许所有请求通过，具体的认证和授权由各微服务处理
        http.authorizeExchange(exchanges -> exchanges.anyExchange().permitAll());

        log.info("Spring Security过滤器链配置完成");
        return http.build();
    }
}